﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Xml;
using System.IO;
using System.Reflection;

namespace TCC.Framework
{
    public class UnityIconsWindow : EditorWindow
    {
        GUIStyle m_iconNameStyle;
        TextEditor m_te = new TextEditor();
        private const float width = 50;

        [MenuItem("TCCFramework/Tools/内置图标查询工具")]
        static void Init()
        {
            EditorWindow window = GetWindow<UnityIconsWindow>();
            window.maxSize = new Vector2(619, 616);
            window.minSize = new Vector2(619, 616);
            window.titleContent.text = "内置图标查询工具";
            window.titleContent.image = EditorGUIUtility.IconContent("Assembly Icon").image;
            window.Show();
        }

        List<Texture2D> builtInTexs = new List<Texture2D>();
        void GetBultinAsset()
        {
            var flags = BindingFlags.Static | BindingFlags.NonPublic;
            var info = typeof(EditorGUIUtility).GetMethod("GetEditorAssetBundle", flags);
            var bundle = info.Invoke(null, new object[0]) as AssetBundle;
            UnityEngine.Object[] objs = bundle.LoadAllAssets();
            if (null != objs)
            {
                for (int i = 0; i < objs.Length; i++)
                {
                    if (objs[i] is Texture2D)
                    {
                        builtInTexs.Add(objs[i] as Texture2D);
                    }
                }
            }
        }

        void OnEnable()
        {
            GetBultinAsset();
            m_iconNameStyle = new GUIStyle("WarningOverlay");
            m_iconNameStyle.fontSize = 12;
        }

        Vector2 scrollPos = Vector2.zero;
        void OnGUI()
        {
            EditorGUILayout.BeginVertical();
            scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
            int count = (int)Mathf.Floor(position.width / (width + 3));
            for (int i = 0; i < builtInTexs.Count; i += count)
            {
                EditorGUILayout.BeginHorizontal();
                for (int j = 0; j < count; j++)
                {
                    int index = i + j;
                    if (index < builtInTexs.Count)
                    {
                        EditorGUILayout.BeginVertical();
                        if (GUILayout.Button(builtInTexs[index], GUILayout.Width(width), GUILayout.Height(width)))
                        {
                            m_te.text = $"EditorGUIUtility.IconContent(\"{builtInTexs[index].name}\").image";
                            m_te.SelectAll();
                            m_te.Copy();
                            ShowNotification(new GUIContent($"已拷贝【{builtInTexs[index].name}】贴图代码"));
                        }
                        GUILayout.Label(builtInTexs[index].name, GUILayout.Width(width));
                        EditorGUILayout.EndVertical();
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
            EditorGUILayout.EndScrollView();
            EditorGUILayout.EndVertical();
        }
    }
}
